package com.project.springboot.service.lock4j.impl;

import com.baomidou.lock.annotation.Lock4j;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.yongzhuzl.commonutil.util.empty.EmptyUtil;
import com.project.springboot.VO.Lock4jUserReqVo;
import com.project.springboot.VO.Lock4jUserResVo;
import com.project.springboot.dataobject.Lock4jUser;
import com.project.springboot.mapper.Lock4jUserMapper;
import com.project.springboot.page.PageQuery;
import com.project.springboot.page.PageResult;
import com.project.springboot.service.lock4j.Lock4jUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * description: Lock4j用户服务接口实现类
 * @author 朱正磊
 * @date 2023-08-08 23:34:24
 */
@Slf4j
@Service("lock4jUserService")
public class Lock4jUserServiceImpl extends ServiceImpl<Lock4jUserMapper, Lock4jUser> implements Lock4jUserService {

    @Resource
    private Lock4jUserMapper lock4jUserMapper;

    /**
     * description: 测试基于Redisson的Lock4j分布式锁框架
     * @author 朱正磊
     * @date 2023-08-08 23:41:25
     * @param lock4jUser 用户实体类
     */
    @Lock4j(keys = {"#lock4jUser.id"})
    @Override
    public void doLock4jTest(Lock4jUser lock4jUser) {
        Integer pointSum = lock4jUserMapper.getPointSum();
        log.info("pointSum = {}", pointSum);
        if (pointSum == null || pointSum <= 18) {
            lock4jUserMapper.insert(lock4jUser);
        }
    }

    /**
     * description: 分页查询用户信息
     * @author 朱正磊
     * @date 2023-08-09 23:42:14
     * @return 返回分页结果对象
     */
    @Override
    public PageResult<Lock4jUserResVo> userPage(Lock4jUserReqVo lock4jUserReqVo) {
        // 方式一
        // return this.userPage1(lock4jUserReqVo);
        // 方式二
        return this.userPage2(lock4jUserReqVo);
    }

    /**
     * description: 分页查询用户信息（方式一）
     * @author 朱正磊
     * @date 2023-08-09 23:42:14
     * @return 返回分页结果对象
     */
    private PageResult<Lock4jUserResVo> userPage1(Lock4jUserReqVo lock4jUserReqVo) {
        LambdaQueryWrapper<Lock4jUser> wrapper = new LambdaQueryWrapper<>();
        if (EmptyUtil.isNotEmpty(lock4jUserReqVo.getName())) {
            wrapper.like(Lock4jUser::getName, lock4jUserReqVo.getName());
        }
        IPage<Lock4jUser> page = this.page(new PageQuery<Lock4jUser>().getPage(), wrapper);
        List<Lock4jUser> lock4jUserList = page.getRecords();
        List<Lock4jUserResVo> resVoList = this.getLock4jUserResVoList(lock4jUserList);
        return new PageResult<>(resVoList, page);
    }

    /**
     * description: 将Lock4jUser转换成Lock4jUserResVo
     * @author 朱正磊
     * @date 2023-08-09 23:56:45
     * @param lock4jUserList Lock4jUser集合
     * @return 返回Lock4jUserResVo集合
     */
    private List<Lock4jUserResVo> getLock4jUserResVoList(List<Lock4jUser> lock4jUserList) {
        List<Lock4jUserResVo> resVoList = new ArrayList<>();
        lock4jUserList.forEach(lock4jUser -> {
            Lock4jUserResVo resVo = new Lock4jUserResVo();
            resVo.setName(lock4jUser.getName());
            resVo.setProvince(lock4jUser.getProvince());
            resVo.setCity(lock4jUser.getCity());
            resVo.setPoint(lock4jUser.getPoint());
            resVoList.add(resVo);
        });
        return resVoList;
    }

    /**
     * description: 分页查询用户信息（方式二）
     * @author 朱正磊
     * @date 2023-08-09 23:42:14
     * @return 返回分页结果对象
     */
    private PageResult<Lock4jUserResVo> userPage2(Lock4jUserReqVo lock4jUserReqVo) {
        IPage<Lock4jUserResVo> page = new PageQuery<Lock4jUserResVo>().getPage();
        IPage<Lock4jUserResVo> ResultPage = lock4jUserMapper.page(page, lock4jUserReqVo);
        return new PageResult<>(ResultPage);
    }

}
